package com.unipay.msm.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.unipay.msm.config.ConstantPropertiesUtils;
import com.unipay.msm.service.MsmService;
import com.unipay.vo.msm.MsmVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-05-04 20:43:39
 * @Desc: TODO
 */
@Service
public class MsmServiceImpl implements MsmService {

    // TODO 通过整合短信服务，进行验证码发送
    @Override
    public boolean send(String phone, String code) {
        // 如果：phone为空，不需要发送（返回false，发送失败）
        if (StringUtils.isEmpty(phone)) {
            return false;
        }

        // TODO 整合阿里云“短信服务”，设置相关参数
        DefaultProfile profile = DefaultProfile.getProfile(
                ConstantPropertiesUtils.REGION_ID,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "Cyu自学项目");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_460755565");
        //验证码  使用json格式   {"code":"123456"}
        Map<String, Object> param = new HashMap();
        param.put("code", code);
        System.out.println("param: " + param);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            // 打印：{"Message":"测试专用签名和模板必须结合使用","RequestId":"925A46E7-E32C-5ED5-811A-AD25B6498825","Code":"isv.SMS_TEST_SIGN_TEMPLATE_LIMIT"}
            System.out.println("response.getData(): " + response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    // mq发送短信封装
    @Override
    public boolean send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())){
            String code = (String) msmVo.getParam().get("code");
            return this.send(msmVo.getPhone(), code);
        }
        return false;
    }

    @Override
    public boolean send(String phone, Map<String,Object> param) {
        // 如果：phone为空，不需要发送（返回false，发送失败）
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        // TODO 整合阿里云“短信服务”，设置相关参数
        DefaultProfile profile = DefaultProfile.getProfile(
                ConstantPropertiesUtils.REGION_ID,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "Cyu自学项目");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_460755565");

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            // 打印：{"Message":"测试专用签名和模板必须结合使用","RequestId":"925A46E7-E32C-5ED5-811A-AD25B6498825","Code":"isv.SMS_TEST_SIGN_TEMPLATE_LIMIT"}
            System.out.println("response.getData(): " + response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
